// pages/need/need.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banner:'/banner7.gif',
    imgs:[],
    name:'',
    idnumber:'',
    address:'',
    phone:'',
    reason:'',
    isshow:0
  },
  chooseImg: function (e) {
    var that = this;
    var imgs = that.data.imgs;
    var length = 4-imgs.length;
    wx.chooseImage({
      count: 4,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        var imgs = that.data.imgs;
        for (var i = 0; i < tempFilePaths.length; i++) {
          if (imgs.length >= 9) {
            that.setData({
              imgs: imgs
            });
            return false;
          } else {
            imgs.push(tempFilePaths);
          }
          var tempFilePaths = res.tempFilePaths;
        }
        that.setData({
          imgs: res.tempFilePaths
        });

      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    return {
      title: '和善龙江 求助',
      path:'/pages/index/index',
       imageUrl: 'http://cdn2.kyhdmedia.com/xcx_ljcs/share.jpg'
    }
  },
  onShareTimeline: function () {
    var that = this;
    return {
      title: '和善龙江 求助',
      path:'/pages/main/main',
       imageUrl: 'http://cdn2.kyhdmedia.com/xcx_ljcs/share.jpg'
    }
  },
  submitData:function(e){
    console.log(e);
    var data = e.detail.value;
    var imgs = this.data.imgs;

    if(data.name=="")
    {
      wx.showToast({
        title: '姓名不能为空',
        icon: 'none',
        duration: 1500
      })
    }else if(data.idnumber=="")
    {
      wx.showToast({
        title: '身份证号码不能为空',
        icon: 'none',
        duration: 1500
      })
    }else  if (!(/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(data.idnumber))) {
      wx.showToast({
        title: '身份证号码格式不正确',
        icon: 'none',
        duration: 2000
      });
    } else if(data.address=="")
    {
      wx.showToast({
        title: '地址不能为空',
        icon: 'none',
        duration: 1500
      })
    }else if(data.phone=="")
    {
      wx.showToast({
        title: '联系电话不能为空',
        icon: 'none',
        duration: 1500
      })
    } else if (!(/^1[345678]\d{9}$/.test(data.phone))) {
      wx.showToast({
        title: '联系电话格式不正确',
        icon: 'none',
        duration: 2000
      });
    } else if(data.reason=="")
    {
      wx.showToast({
        title: '求助原因不能为空',
        icon: 'none',
        duration: 1500
      })
    }else if(imgs.length<=0)
    {
      wx.showToast({
        title: '至少上传一张资料相片',
        icon: 'none',
        duration: 1500
      })
    }else{
      this.upload(data);
    }
  },
  upload: function (e) {
    console.log('上传图片至服务器');
    var photo_info = e.photo_info;
    var timestamp = e.timestamp;
    var i = e.i;
    var that = this;
    var length = that.data.imgs.length;
    //for (var i = 0; i < length; i++) {//循环遍历图片    //ps::这里也可以使用for循环方式一个个上传，但是因为网络等原因，不能很好地控制，故改用这种重新调用的方式来完成
        var openid = wx.getStorageSync("openid");
        for (let index = 0; index < length; index++) {
          wx.uploadFile({
            url: app.globalData.baseUrl + '/api/index/applyDonate',
            filePath: that.data.imgs[index],
            name: 'photos',
            formData:({//上传图片所要携带的参数
                openid:openid,
                name:e.name,
                id_card:e.idnumber,
                address:e.address,
                introduction:e.reason,
                mobi:e.phone
            }),
        success: function (res) {
          
                wx.showToast({
                    title: '上传中...',
                    duration: 2000,
                    icon: 'loading'
                });


                  setTimeout(function(){
                    wx.showToast({
                      title: '上传成功',
                      duration: 1000,
                  });
                  that.setData({
                      imgs:[],
                      name:'',
                      idnumber:'',
                      address:'',
                      phone:'',
                      reason:''
                        })

                  },3000);
                  

                
            
        },
        complete:function(res){
            console.log(res);
          
        }
        })
          
        }
       
    //}
},
  deleteImg: function (e) {
    var imgs = this.data.imgs;
    var index = e.currentTarget.dataset.index;
    imgs.splice(index, 1);
    this.setData({
      imgs: imgs
    });
  },
  previewImg: function (e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index;
    //所有图片
    var imgs = this.data.imgs;
    wx.previewImage({
      //当前显示图片
      current: imgs[index],
      //所有图片
      urls: imgs
    })
  },
})